package member

import (
	"github.com/gin-gonic/gin"
	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
)

func GetMember(c *gin.Context) {
	var data models.Member
	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		app.CommonResp(c, 126, "当前用户未登陆", nil)
		return
	}
	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, 126, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	// 跟新
	if result.MomUserId != 0 && result.MomUserCode == "" {
		var dm models.Member
		dmInfo, _ := dm.GetByID(result.MomUserId)
		result.MomUserCode = dmInfo.MemberCode
		_, _ = result.Update(result.MemberId)
	}

	openId := c.Request.FormValue("openId")

	var thirdI models.ThirdUser

	if openId != "" {
		thirdI.ThridOpenId = openId
		result.ThirdUserInfo, _ = thirdI.Get()
	} else {

		var third models.MemberThirdInfo
		third.MemberId = result.MemberId
		th, err := third.Get()
		if err == nil {
			thirdI.ThridUserId = th.ThirdUserId
			result.ThirdUserInfo, _ = thirdI.Get()
		}
	}

	//lognew.Info(c, "GetMember", lognew.Fields{"info": result})
	if result.MomUserId != 0 {
		go UpdateMomLevel(result.MomUserId)
	}

	app.OK(c, result, "成功")
}
